package com.mhz.echartsdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mhz.echartsdemo.pojo.EduSubject;
import com.mhz.echartsdemo.mapper.EduSubjectMapper;
import com.mhz.echartsdemo.pojo.StatisticsDaily;
import com.mhz.echartsdemo.pojo.subject.SubjectOne;
import com.mhz.echartsdemo.pojo.subject.SubjectThree;
import com.mhz.echartsdemo.pojo.subject.SubjectTwo;
import com.mhz.echartsdemo.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author mhz
 * @since 2021-04-01
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public Map<String, Object> getChartData() {

        //先查询一级分类
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<EduSubject> eduSubjectOne = baseMapper.selectList(wrapper);

        //查询二级分类
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id",0);
        List<EduSubject> eduSubjectsTwo = baseMapper.selectList(wrapper2);
        List<String> subjectTitle = new ArrayList<>();
        List<Integer> subjectCount = new ArrayList<>();
        for (EduSubject subjectOne : eduSubjectOne) {
            subjectTitle.add(subjectOne.getTitle());
            int count = 0;
            String id = subjectOne.getId();
            for (EduSubject subjectTwo : eduSubjectsTwo) {
                if (subjectTwo.getParentId().equals(id)){
                    count++;
                }
            }
            subjectCount.add(count);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("subject",subjectTitle);
        map.put("count",subjectCount);
        return map;
    }

    @Override
    public SubjectOne getTreeData() {

        //先设置一级目录
        SubjectOne subjectOne = new SubjectOne();
        subjectOne.setName("计算机必修课");
        //查询二级目录
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<EduSubject> eduSubjectTwo = baseMapper.selectList(wrapper);

        //查询三级目录
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id",0);
        List<EduSubject> eduSubjectsThree = baseMapper.selectList(wrapper2);
        //给二级目录name属性赋值
        List<SubjectTwo> subjectTwoList = new ArrayList<>();
        for (EduSubject subject : eduSubjectTwo) {
            SubjectTwo subjectTwo = new SubjectTwo();
            subjectTwo.setName(subject.getTitle());
            String id = subject.getId();
            List<SubjectThree> subjectThreeList = new ArrayList<>();

            for (EduSubject eduSubject : eduSubjectsThree) {
                if (eduSubject.getParentId().equals(id)) {
                    SubjectThree subjectThree = new SubjectThree();
                    subjectThree.setName(eduSubject.getTitle());
                    subjectThreeList.add(subjectThree);
                }
            }
            subjectTwo.setChildren(subjectThreeList);
            subjectTwoList.add(subjectTwo);
        }
        subjectOne.setChildren(subjectTwoList);

        return subjectOne;
    }
}
